﻿using Domain.User;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Account;
using MES.Server.API.Dto.Department;
using ReturnValue;

namespace MES.Server.API.Application.Handler.Account
{
    public class GetDepartmentHandler : IRequestHandler<GetDepartmentCommand, ApiResult<List<DepartmentDTO>>>
    {
        private readonly IRepository<DepartmentModel> departRepo;

        public GetDepartmentHandler(IRepository<DepartmentModel> departRepo)
        {
            this.departRepo = departRepo;
        }
        public Task<ApiResult<List<DepartmentDTO>>> Handle(GetDepartmentCommand request, CancellationToken cancellationToken)
        {
            var departments = departRepo.GetAll().Where(x => !x.IsDel);
            var linq = from a in departments
                       select new DepartmentDTO()
                       {
                           Id = a.Id,
                           DepartmentName = a.DepartmentName
                       };
            var res = new ApiResult<List<DepartmentDTO>>();
            res.Message = "部门查询成功";
            res.Data = linq.ToList();
            res.Code = StatusCode.Success;
            return Task.FromResult(res);
        }
    }
}
